home *** CD-ROM | disk | FTP | other *** search
- /* main.cpp
- *
- * This is the main entry point for the code to strip the 68K resources
- * from a fat binary. This code will help reduce the amount of space that
- * is used inside of an application on disk.
- *
- * Code created by
- * William Edward Woody
- * In Phase Consulting
- * 337 West California #4
- * Glendale, CA 91203
- */
-
- #include "Strip.h"
-
- #include <AppleEvents.h>
- #include <GestaltEqu.h>
-
- /************************************************************************/
- /* */
- /* Globals. */
- /* */
- /************************************************************************/
-
- int FAppleEvents;
- int FHasFSSpec;
- int FColorQuickdraw;
- AEEventHandlerUPP PAppleEventProcessProc;
- int QuitFlag = 0;
- int RunEventLoop = 0;
- int SawNewEvent = 0;
- int DoneEvent;
-
- /************************************************************************/
- /* */
- /* Generic application initialization. */
- /* */
- /************************************************************************/
-
- /* InitMacintoshOS
- *
- * This does the required minimum of initialization for the Macintosh
- * operating system.
- */
-
- static void InitMacintoshOS(void)
- {
- InitGraf(&qd.thePort); /* Init quickdraw display */
- InitFonts(); /* Initialize fonts */
- InitWindows(); /* Initialize windows */
- InitMenus(); /* Initialize menu mangler */
- TEInit(); /* Initialize text editor */
- InitDialogs(NULL); /* Initialize dialog box */
-
- FlushEvents(everyEvent,0); /* Flush all events */
- InitCursor(); /* Put up arrow cursor */
- }
-
- /* InitMenuBar
- *
- * This initializes the menu bar
- */
-
- static void InitMenuBar(void)
- {
- MenuHandle mh;
- Handle menuBar;
-
- menuBar = GetNewMBar(128);
- SetMenuBar(menuBar);
-
- mh = GetMenu(MApple);
- AddResMenu(mh,'DRVR');
-
- DrawMenuBar();
- }
-
- /* GetMacintoshEnvironment
- *
- * Get the current operating system environment state
- */
-
- static void GetMacintoshEnvironment(void)
- {
- long val;
-
- /*
- * Color Quickdraw? -- System 5.0 or greater
- */
-
- if (!Gestalt(gestaltQuickdrawVersion,&val)) {
- FColorQuickdraw = (val >= 0x100) ? 1 : 0;
- } else {
- FColorQuickdraw = 0;
- }
-
- /*
- * Apple Events? -- System 7.0 or greater
- */
-
- if (!Gestalt(gestaltAppleEventsAttr,&val)) {
- FAppleEvents = (val & (1 << gestaltAppleEventsPresent)) ? 1 : 0;
- } else {
- FAppleEvents = 0;
- }
-
- /*
- * FSSpec files? -- System 7.0 or greater
- */
-
- if (!Gestalt(gestaltFSAttr,&val)) {
- FHasFSSpec = (val & (1 << gestaltHasFSSpecCalls)) ? 1 : 0;
- } else {
- FHasFSSpec = 0;
- }
- }
-
- /************************************************************************/
- /* */
- /* System 7 (Apple Event) support. */
- /* */
- /************************************************************************/
-
- /* GotRequiredParams
- *
- * Were all of the required parameters returned?
- */
-
- static OSErr GotRequiredParams(AppleEvent *event)
- {
- OSErr myErr;
- long size;
- DescType returnType;
-
- myErr = AEGetAttributePtr(event,keyMissedKeywordAttr,typeWildCard,&returnType,NULL,0,&size);
- if (myErr == noErr) return errAEParamMissed;
- else if (myErr == errAEDescNotFound) return noErr;
- else return myErr;
- }
-
- /* AppleEventProcessProc
- *
- * This is the procedure used to process an apple event.
- */
-
- static pascal OSErr AppleEventProcessProc(AppleEvent *event, AppleEvent *, long)
- {
- OSErr myErr;
- long eClass,eType;
- DescType returnType;
- long size;
- long off,len;
- AEDescList docList;
- AEKeyword keyWord;
- FSSpec file;
-
- /*
- * Get the Apple Event message type
- */
-
- if (noErr != (myErr = AEGetAttributePtr(event,keyEventClassAttr,typeType,&returnType,(Ptr)&eClass,sizeof(eClass),&size))) return myErr;
- if (noErr != (myErr = AEGetAttributePtr(event,keyEventIDAttr,typeType,&returnType,(Ptr)&eType,sizeof(eType),&size))) return myErr;
-
- /*
- * We've now got the class and type of the event. Dispatch for the
- * common four entry points.
- */
-
- if (eClass == kCoreEventClass) {
- switch (eType) {
- case kAEOpenApplication:
- /*
- * New application event
- */
-
- myErr = GotRequiredParams(event);
- if (myErr == noErr) {
- SawNewEvent = 1;
- InitMenuBar();
- }
- return myErr;
- case kAEOpenDocuments:
- /*
- * Open new documents
- */
-
- if (noErr != (myErr = AEGetParamDesc(event,keyDirectObject,typeAEList,&docList))) return myErr;
- if (noErr != (myErr = AECountItems(&docList,&len))) {
- AEDisposeDesc(&docList);
- return myErr;
- }
- for (off = 1; off <= len; off++) {
- if (noErr != (myErr = AEGetNthPtr(&docList,off,typeFSS,&keyWord,&returnType,(Ptr)&file,sizeof(file),&size))) {
- AEDisposeDesc(&docList);
- return myErr;
- }
-
- StripFile(&file);
- if (!SawNewEvent) RunEventLoop = 0; // Stop running on first NULL
- }
- AEDisposeDesc(&docList);
- break;
- case kAEQuitApplication:
- /*
- * Quit this application
- */
-
- myErr = GotRequiredParams(event);
- if (myErr == noErr) {
- QuitFlag = 1;
- return noErr;
- } else return myErr;
- default:
- return errAEEventNotHandled; // Not able to handle event
- }
-
- return GotRequiredParams(event); // Fall through to handle this
- }
-
- /*
- * Process the rest of the possible events
- */
-
- return errAEEventNotHandled;
- }
-
- /* InitAppleEvents
- *
- * This does the required initialization to install the above routine
- * into the list of apple event procedures
- */
-
- static void InitAppleEvents(void)
- {
- PAppleEventProcessProc = NewAEEventHandlerProc(AppleEventProcessProc);
- AEInstallEventHandler(typeWildCard,typeWildCard,PAppleEventProcessProc,0,false);
- }
-
- /* EndAppleEvents
- *
- * Dispose of the apple events manager proc
- */
-
- static void EndAppleEvents(void)
- {
- DisposeRoutineDescriptor(PAppleEventProcessProc);
- }
-
- /************************************************************************/
- /* */
- /* About Me Dialog Box Manager */
- /* */
- /************************************************************************/
-
- static short GRedraw = 0;
-
- /* DrawVersionString
- *
- * This function (which can be overridden by the user) draws the
- * version if the version resource exists
- */
-
- static void DrawVersionString(Rect *r)
- {
- FontInfo finfo;
-
- struct VersRecord {
- unsigned char majVersion;
- unsigned char minVersion;
- unsigned char devel;
- unsigned char release;
- short region;
- unsigned char shortVers[2];
- } **vers;
- short x,y;
-
- if (NULL == (vers = (VersRecord **)GetResource('vers',1))) return;
- HLock((Handle)vers);
-
- /*
- * Print the version string (the pascal string contained in this thing)
- */
-
- TextFont(4);
- TextSize(9);
- TextMode(srcOr);
- GetFontInfo(&finfo);
- y = r->top + finfo.ascent;
- x = r->right - StringWidth((**vers).shortVers);
- MoveTo(x,y);
- DrawString((**vers).shortVers);
-
- HUnlock((Handle)vers);
- ReleaseResource((Handle)vers);
-
- TextFont(0);
- TextSize(12);
- }
-
- /* AboutMeFilter
- *
- * This is the filter procedure for simply waiting around until the
- * user does something.
- */
-
- static pascal Boolean AboutMeFilter(DialogPtr dlog, EventRecord *event, short *item)
- {
- if (GRedraw) {
- SetPort(dlog);
- DrawVersionString(&(dlog->portRect));
- GRedraw = 0;
- }
-
- if ((event->what == mouseDown) || (event->what == keyDown)) {
- *item = 1;
- return 1;
- } else if (event->what == updateEvt) {
- GRedraw = 1;
- return 0;
- } else return 0;
- }
-
- /* OpenAboutMe
- *
- * Open the about me dialog box
- */
-
- static DialogPtr OpenAboutMe(void)
- {
- DialogPtr dlog;
- GrafPtr port;
- Rect r;
- short x,y;
-
- if (FColorQuickdraw) {
- dlog = GetNewDialog(ColorAboutMe,NULL,(WindowPtr)-1);
- if (dlog == NULL) {
- dlog = GetNewDialog(BWAboutMe,NULL,(WindowPtr)-1);
- if (dlog == NULL) {
- return NULL;
- }
- }
- } else {
- dlog = GetNewDialog(BWAboutMe,NULL,(WindowPtr)-1);
- if (dlog == NULL) {
- return NULL;
- }
- }
-
- GetWMgrPort(&port); // Center the window
- r = port->portRect;
- x = r.right - r.left;
- y = r.bottom - r.top;
- r = dlog->portRect;
- x -= r.right - r.left;
- y -= r.bottom - r.top;
- x /= 2;
- y /= 2;
- MoveWindow(dlog,x,y,0);
-
- ShowWindow(dlog);
- SetPort(dlog);
-
- return dlog;
- }
-
- /* DoAboutMe
- *
- * how to run a dialog box
- */
-
- static void DoAboutMe(void)
- {
- short i;
- ModalFilterUPP proc;
-
- DialogPtr dlog = OpenAboutMe();
- if (!dlog) return;
- proc = (ModalFilterUPP)NewRoutineDescriptor((ProcPtr)AboutMeFilter,uppModalFilterProcInfo,GetCurrentISA());
-
- for (;;) {
- ModalDialog(proc,&i);
- if (i == 1) break;
- }
-
- DisposeRoutineDescriptor(proc);
- DisposeDialog(dlog);
- }
-
- /************************************************************************/
- /* */
- /* Strip Dialog */
- /* */
- /************************************************************************/
-
- /* DoStripDialog
- *
- * Ask the user to open an application and strip it by calling the
- * strip routine
- */
-
- static void DoStripDialog(void)
- {
- SFTypeList files = { 'APPL', NULL, NULL, NULL };
-
- if (FHasFSSpec) {
- StandardFileReply reply;
-
- StandardGetFile(NULL,1,files,&reply);
- if (reply.sfGood) StripFile(&reply.sfFile);
- } else {
- SFReply reply;
- Point pt;
- FSSpec file;
- long procID;
-
- pt.h = 75;
- pt.v = 75;
- SFGetFile(pt,NULL,NULL,1,files,NULL,&reply);
- if (!reply.good) return;
-
- procID = 0;
- file.vRefNum = 0;
- BlockMove(reply.fName,file.name,1+reply.fName[0]);
- GetWDInfo(reply.vRefNum,&file.vRefNum,&file.parID,&procID);
- StripFile(&file);
- }
- }
-
- /************************************************************************/
- /* */
- /* Event Management */
- /* */
- /************************************************************************/
-
- /* GetEvent
- *
- * Get the next event in a reasonable way
- */
-
- static int GetEvent(EventRecord *event)
- {
- return WaitNextEvent(everyEvent,event,GetCaretTime(),NULL);
- }
-
- /* DoMenu
- *
- * Handle dispatching the menu item to the menu manager
- */
-
- static void DoMenu(long menuID)
- {
- short hi,lo;
- MenuHandle mh;
- unsigned char buffer[256];
-
- hi = (short)(menuID >> 16);
- lo = (short)(menuID & 0x0FFFF);
-
- switch (hi) {
- case MApple:
- switch (lo) {
- case MAppleAboutMe:
- DoAboutMe();
- break;
- default:
- mh = GetMHandle(MApple);
- GetItem(mh,lo,buffer);
- OpenDeskAcc(buffer);
- break;
- }
- break;
- case MFile:
- switch (lo) {
- case MFileStrip:
- DoStripDialog();
- break;
- case MFileQuit:
- QuitFlag = 1;
- break;
- }
- break;
- case MEdit:
- SystemEdit(lo-1);
- break;
- }
- }
-
- /* DoMouse
- *
- * Handle mouse down event
- */
-
- static void DoMouse(EventRecord *e)
- {
- short i;
- WindowPtr w;
-
- i = FindWindow(e->where,&w); // Get current window
-
- switch (i) {
- case inMenuBar: // In the menu bar
- DoMenu(MenuSelect(e->where));
- break;
- case inSysWindow: // In a desk accessory
- SystemClick(e,w);
- break;
- case inContent: // Click in content
- SelectWindow(w);
- break;
- }
- }
-
- /* DoEvent
- *
- * What to do with this event
- */
-
- static void DoEvent(EventRecord *e)
- {
- int err;
-
- switch (e->what) {
- case nullEvent:
- if (!RunEventLoop) QuitFlag = 1; // Don't run this application
- break;
- case mouseDown:
- DoMouse(e);
- break;
- case keyDown:
- case autoKey:
- if (e->modifiers & cmdKey) {
- if (e->what == keyDown) {
- DoMenu(MenuKey((char)(e->message & charCodeMask)));
- }
- }
- break;
- case kHighLevelEvent:
- err = AEProcessAppleEvent(e); // Dispatch it...
- break;
- }
- HiliteMenu(0);
- }
-
- /************************************************************************/
- /* */
- /* Main entry point */
- /* */
- /************************************************************************/
-
- /* main
- *
- * Where it all starts.
- */
-
- int main()
- {
- EventRecord event;
-
- InitMacintoshOS();
- GetMacintoshEnvironment();
-
- QuitFlag = 0;
- if (FAppleEvents) {
- RunEventLoop = 1;
- SawNewEvent = 0;
- InitAppleEvents();
- } else {
- RunEventLoop = 1;
- SawNewEvent = 1;
- InitMenuBar();
- }
-
- while (!QuitFlag) {
- GetEvent(&event);
- DoEvent(&event);
- }
-
- EndAppleEvents();
- return 0;
- }
-